import winim/lean
import winim/inc/winuser
import std/[times, os, strutils]
# import std/random

var
  lis: array[0..2, string] = ["#签到", "#打卡", "#美滋滋"] 
  lis1: array[0..9, string] = ["鱼子酱", "大酱汤", "炒乌冬", "乌冬面", "醋饭", "鲷鱼烧", "拉面", "海鲜汤", "牛尾汤", "橙醋蟹"]
  # y = @[1, 2, 3, 4, 5, 6]
let wname1: LPCWSTR = L"Mozilla Firefox"
let winclass: LPCWSTR = L"MozillaCompositorWindowClass"
var
  dt = dateTime(1970, mJan, 01, 00, 00, 00, 00, utc())
  tm = dt.toTime() 
 
doAssert format(tm, "yyyy-MM-dd'T'HH:mm:ss", utc()) == "1970-01-01T00:00:00"

# echo lis
# echo lis1  

# proc testOpenArray(x: openArray[string]) = echo repr(x)
# proc testOpenArray(x: openArray[string]) = echo x

# testOpenArray(lis)
# testOpenArray(lis1)

proc time_count(interval:int = 2) = 
  var
    t1 = now()
    t2 = now() + initDuration(minutes = interval) 
  echo "current time[$1]" % [t1.format("HH:mm:ss")]
  echo "\n[$#] would be coming." % [t2.format("HH:mm:ss")]

proc mykey_press_release_u(keyno: int = 1) = 
  # key 1 : 0x31 or 49
  # key 2 : 0x32 or 50
  # key 3 : 0x33 or 51
  # ctrl  : 0x11 or 17
  # alt   : 0x12 or 18

  # 按下ctrl+v
  # win32api.keybd_event(0x11, 0, 0, 0)
  # win32api.keybd_event(0x56, 0, 0, 0)
  # win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
  # win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
  # time.sleep(1)
  # # 按下回车
  # win32api.keybd_event(0x0D, 0, 0, 0)
  # win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
  if 1 == keyno:
    keybd_event(0x11, 0, 0, 0)
    keybd_event(0x31, 0, 0, 0)
    keybd_event(0x31, 0, KEYEVENTF_KEYUP, 0)
    keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0)
  elif 2 == keyno:
    keybd_event(0x11, 0, 0, 0)
    keybd_event(0x32, 0, 0, 0)
    keybd_event(0x32, 0, KEYEVENTF_KEYUP, 0)
    keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0)

proc getContentFromClipboared(hData, phw:HWND) = 
  # content = lis[0]
  # initil hData
  # hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(content)+1)
  # pData = cast[LPWSTR](GlobalLock(hData))
  # lstrcpy(pData, content)
  # GlobalUnlock(hData)

  OpenClipboard(phw)
  EmptyClipboard()
  # CF_TEXT 
  # SetClipboardData(CF_TEXT, hData)
  # GetClipboardData(CF_TEXT)
  SetClipboardData(CF_UNICODETEXT, hData)
  GetClipboardData(CF_UNICODETEXT)
  CloseClipboard()
  SendMessage(phw, WM_PASTE, 0, 0)
  sleep(1000)
  # Ctrl+V
  keybd_event(0x11, 0,0,0)
  keybd_event(0x56, 0,0,0)
  keybd_event(0x56, 0, KEYEVENTF_KEYUP,0)
  keybd_event(0x11, 0, KEYEVENTF_KEYUP,0)

proc findWindowFromPoint(keyno: int = 1,iinterval:int =2) = 
  var 
    phw: HWND

  var
    pointold: LPPOINT
    point: POINT
    wintitle: LPWSTR
    isOk: bool

  var
    # content: seq[LPWSTR]
    pData, content: LPWSTR
    hData: HWND
    lpRect: LPRECT

  content = lis[0]
  # content = "加油" & ","
  if 2 == keyno:
    # content = "#打卡"
    content = lis[2]
  # initil hData
  hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(content)+2)
  pData = cast[LPWSTR](GlobalLock(hData))
  lstrcpy(pData, content)
  GlobalUnlock(hData)

  point.x = 1347
  point.y = 761  
  GetCursorPos(pointold)
  mykey_press_release_u(keyno)
  sleep(4000)
  time_count(iinterval)
  sleep(1000)
  phw = WindowFromPoint(cast[POINT](pointold))
  ShowWindow(phw, SW_SHOW)
  SetCursorPos(1347, 761)
  mouse_event(MOUSEEVENTF_LEFTDOWN, 1394, 764, 0, 0)
  mouse_event(MOUSEEVENTF_LEFTUP, 1394, 764, 0, 0)
  sleep(2000)

  # OpenClipboard(phw)
  # EmptyClipboard()
  # # CF_TEXT 
  # # SetClipboardData(CF_TEXT, hData)
  # # GetClipboardData(CF_TEXT)
  # SetClipboardData(CF_UNICODETEXT, hData)
  # GetClipboardData(CF_UNICODETEXT)
  # CloseClipboard()
  # SendMessage(phw, WM_PASTE, 0, 0)
  # sleep(1000)
  # # Ctrl+V
  # keybd_event(0x11, 0,0,0)
  # keybd_event(0x56, 0,0,0)
  # keybd_event(0x56, 0, KEYEVENTF_KEYUP,0)
  # keybd_event(0x11, 0, KEYEVENTF_KEYUP,0)
  getContentFromClipboared(hData, phw)
  sleep(1000)
  # Enter Key Press
  keybd_event(VK_RETURN, 0,0,0)
  keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP,0)
  # SendMessage(phw, WM_KEYDOWN, VK_RETURN, 0)
  # SendMessage(phw, WM_KEYUP, VK_RETURN, 0)
  sleep(1000)

echo "--------start--------"
findWindowFromPoint(2, 35)
sleep(2)
proc do_task() =
  var n = 0
  while n < 42:
    if n mod 2 == 0:
      findWindowFromPoint(1, 20)
      sleep(1000 * 60 * 20)
    elif n mod 2 == 1:
      findWindowFromPoint(2, 35)
      sleep(1000 * 60 * 35)
    inc n
    
do_task()    
# for i in 0..7:
#   content = lis1[rand(0..9)] & ","
#   hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(content)+1)
#   pData = cast[LPWSTR](GlobalLock(hData))
#   lstrcpy(pData, content)
#   GlobalUnlock(hData)

#   OpenClipboard(phw)
#   EmptyClipboard()
#   # CF_TEXT 
#   # SetClipboardData(CF_TEXT, hData)
#   # GetClipboardData(CF_TEXT)
#   SetClipboardData(CF_UNICODETEXT, hData)
#   GetClipboardData(CF_UNICODETEXT)
#   CloseClipboard()
#   SendMessage(phw, WM_PASTE, 0, 0)
#   sleep(1000)
#   # Ctrl+V
#   keybd_event(0x11, 0,0,0)
#   keybd_event(0x56, 0,0,0)
#   keybd_event(0x56, 0, KEYEVENTF_KEYUP,0)
#   keybd_event(0x11, 0, KEYEVENTF_KEYUP,0)
#   sleep(1000)
#   # Enter Key Press
#   keybd_event(VK_RETURN, 0,0,0)
#   keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP,0)
#   # SendMessage(phw, WM_KEYDOWN, VK_RETURN, 0)
#   # SendMessage(phw, WM_KEYUP, VK_RETURN, 0)
#   sleep(6000)


